﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Obvious.Framework.Notification.Abstracts;
using Obvious.Framework.Notification.Interfaces;
using System.Net.Mail;
using System.Net;

namespace Obvious.Framework.Notification.Implementation
{
    internal class EmailNotificationAbstractProvider : NotificationProvider
    {
        #region Private Properties
        private string smtpHost;
        private int smtpPort;
        private string smtpUserName;
        private string smtpPassword;
        SmtpClient smtpClient;
        #endregion

        #region Private Properties
        SmtpClient SmtpClient
        {
            get
            {
                if (smtpClient == null)
                {
                    smtpClient = new SmtpClient(smtpHost);
                    smtpClient.Port = smtpPort;
                    if (smtpUserName != null && smtpUserName != string.Empty)
                    {
                        NetworkCredential credential = new NetworkCredential(smtpUserName, smtpPassword);
                        smtpClient.Credentials = credential;
                    }
                }
                return smtpClient;
            }
        }
        #endregion

        #region Constructor
        internal EmailNotificationAbstractProvider()
        {

        }
        internal EmailNotificationAbstractProvider(string configName) : base(configName)
        {
            SetConfig();
        }
        #endregion

        #region NotificationProvider Members
        public override bool Notify(INotification notification)
        {
            return this.SendMail(notification.To, notification.From, notification.Subject, notification.Message);
        }

        public override bool Notify(string To, string From, string Subject, string Message)
        {
            return this.SendMail(To, From, Subject, Message);
        }

        public override bool Notify(string To, string From, string Subject, string Message, DateTime ScheduleSendDate)
        {
            bool bRetValue = true;
            if (DateTime.Now.Date.CompareTo(ScheduleSendDate.Date) <= 0)
                bRetValue = this.SendMail(To, From, Subject, Message);
            //else
            //write logic to put it in queue
            return bRetValue;
        }
        #endregion

        #region Private Members

        private void SetConfig()
        {
            smtpHost = this.ConfigElement.SmtpHost;
            smtpPort = this.ConfigElement.SmtpPort;
            smtpUserName = this.ConfigElement.SmtpUserName;
            smtpPassword = this.ConfigElement.SmtpPwd;
        }

        private bool SendMail(string To, string From, string Subject, string Message)
        {
            bool bRetValue = false;
            //try
            //{
                MailMessage message = new MailMessage(From, To, Subject, Message);
                SmtpClient.Send(message);
            //}
            /*catch (Exception ex)
            {
                bRetValue = true;
            }*/
            bRetValue = true;
            return bRetValue;
        }
        #endregion
    }
}
